Skip to content

Conversation

cmaloney
Copy link
Contributor

@cmaloney cmaloney commented Oct 2, 2025

These don't "redirect" like a terminal where typically the file pointed to by fd 1 and 2 are made the "same" (ex. by dup2 the given fd into that specific fd number). Rather, these context managers set the respective globals to the provided value. In most cases that should be a "file object".


📚 Documentation preview 📚: https://cpython-previews--139490.org.readthedocs.build/

These don't "redirect" like a terminal where typically the file pointed
to by fd 1 and 2 are made the "same" by the preparing process (ex. by
`dup2` the given fd into that specific fd number). Rather, these context
managers set the respective globals to the provided value. In most cases
that should be a "file object".
@cmaloney
Copy link
Contributor Author

cmaloney commented Oct 3, 2025

@wolfgang-eder how does this look to you?

@rhettinger rhettinger self-assigned this Oct 6, 2025
@rhettinger
Copy link
Contributor

Please limit the edit to just :term:file object.

@wolfgang-eder
Copy link

Thank you for improving the documentation. I am happy with the changes

Similar to :func:`~contextlib.redirect_stdout` but redirecting
:data:`sys.stderr` to another file or file-like object.
Similar to :func:`~contextlib.redirect_stdout` but redirecting the global
:data:`sys.stderr` to another value, typically a :term:`file object`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "another target" would be better here than "another value"?

@cmaloney
Copy link
Contributor Author

cmaloney commented Oct 6, 2025

@rhettinger to check my understanding: reduce to just the "file or file-like object." -> :term:file object; don't add new example or expand on behavior description.

is hardwired to stdout.
is hardwired to :data:`sys.stdout`. This does not modify underlying file
objects or file descriptors. It sets the global :data:`sys.stdout` to the
provided value and at context exit sets it to the previous value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be left out entirely. That is already covered by the docs for sys.stdout and sys.__stdout__

with open(os.devnull, 'w') as devnull:
with redirect_stdout(devnull):
help(pow)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer that this be omitted as well. It stretches well beyond the original bug report. Also, it is not a practical example — the help function has no side-effects, so there is no reason to call it and ignore its output.

@rhettinger
Copy link
Contributor

The docs for redirect_stdout and redirect_stderr should not be too ambitious. The underlying code is very simple. The principal action is setting sys.stdout = new_target on the way in and sys.stdout = original_target on the way out.

The tool is a minor convenience that looks a little nicer than an equivalent try/finally. Let's not make these context managers seem more complicated than they actually are.

@cmaloney
Copy link
Contributor Author

cmaloney commented Oct 8, 2025

Thanks for the rationale! Will keep in mind for other docs changes I work on. Updated for the latest review round.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

4 participants